// map.js
import qqMap from '../../../utils/qqmap'

Page({
  data: {
    latitude: 40.22077,
    longitude: 116.23128,
    markers: [
      {
        id: 1,
        latitude: 40.22077,
        longitude: 116.23128,
        width: 24,
        height: 30
      },
      {
        id: 2,
        latitude: 40.225857999999995,
        longitude: 116.23246699999999,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      }
    ],
    id: ''
  },
  onLoad ({ id }) {
    this.setData({ id })
    this.getRepairDetaild(id)
    this.getPolyline()
  },
  async getRepairDetaild (id) {
    if (!id) return wx.utils.toast('参数有误!')

    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)

    if (code !== 10000) return wx.utils.toast()

    this.setData({ repairDetail })
  },
  getPolyline () {
    qqMap.direction({
      mode: 'bicycling',
      from: '40.225857999999995,116.23246699999999',
      to: '40.22077,116.23128',
      success: res => {
        const ret = res
        const coors = ret.result.routes[0].polyline,
          points = []
        const kr = 1000000
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr
        }

        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }

        this.setData({
          polyline: [
            {
              points,
              width: 4,
              color: '#087'
            }
          ]
        })
      }
    })
  },
  async cancelRepair () {
    const { code } = await wx.http.put('/cancel/repaire/' + this.data.id)
    if (code !== 10000) return wx.utils.toast()
    wx.navigateBack({
      delta: 1
    })
  },
  editRepair () {
    wx.reLaunch({
      url: '../form/index?id=' + this.data.id
    })
  }
})
